<script setup>

import { ref } from 'vue'
import {selectCourseListService,
    updateCourseListService,
    insertCourseListService
} from '@/api/CourseManager.js'
const total = ref(0)
const params = ref({
    courseName:'',
    teacherName:'',
  pageNum:1,
  pageSize:10
})
const handleClick = () => {
  console.log('click')
}

const xcCourseList = ref([])
const xcCourseVoRef = ref()
const xcCourseVo = ref({
      id:'',
      userId:'',
      courseName:'',
      courseDesc:'',
      courseType:'',
      courseTypeStr:'',
      courseStar:'',
      coursePoint:'',
      teacherName:'',
      courseTime:'',
      courseTimeStr:'',
      createTime:'',
      createTimeStr:'',
      courseSubTotal:'',
})
const rules = {
    userId:[{required:true,message:'请输入老师编号',trigger:'blur'}],
    courseName:[{required:true,message:'请输入课程名称',trigger:'blur'}],
    courseType:[{required:true,message:'请输入课程类型',trigger:'blur'}],
    courseStar:[{required:true,message:'请输入课程推荐值',trigger:'blur'}],
    coursePoint:[{required:true,message:'请输入课程分值',trigger:'blur'}],
    teacherName:[{required:true,message:'请输入老师名称',trigger:'blur'}],
    courseTime:[{required:true,message:'请输入开课时间',trigger:'blur'}],
}

const selectCourseList = async()=> {
   const res = await selectCourseListService(params.value)
   xcCourseList.value = res.data.data
   total.value = res.data.total
}
selectCourseList()

const showDrawer = ref(false)

const editInsertBtn = () => {
    subCourse.value = true
    showDrawer.value = true
    xcCourseVo.value={}
}

const isSwitchDisabled = ref(false)
const subCourse = ref(true)
const confirmClick = async()=>{

    await xcCourseVoRef.value.validate()
    if(xcCourseVo.value.id){
        await updateCourseListService(xcCourseVo.value)
    }else{
        await insertCourseListService(xcCourseVo.value)
    }
  
    ElMessage.success('成功！')
    xcCourseVo.value={}
    showDrawer.value =  false
    selectCourseList()
}
const editRow = (row)=>{
    subCourse.value = false
    xcCourseVo.value=row
    isSwitchDisabled.value=false
    showDrawer.value = true

}

const search = ()=> {
    selectCourseList()
}
</script>

<template>
  <div>
    <div class="cus-header">
        <el-card>
            <div style="display: flex; justify-content: space-between;">
                <div class="search">
                    <div>
                        课程：
                        <el-input v-model="params.courseName" style="width: 240px" placeholder="请输入课程名称" />
                    </div>
                    <div style="margin-left: 20px;">
                        老师：
                        <el-input v-model="params.teacherName" style="width: 240px" placeholder="请输入姓名" />
                    </div>
                    <div style="margin-left: 20px;">
                        <el-button @click="search" style="width: 80px;" type="primary">搜索</el-button>
                    </div>
                </div>
                <div class="insert">
                    <el-button style="width: 80px;" type="primary" @click="editInsertBtn">新增</el-button>
                </div>
            </div>
        
            
        </el-card>
    </div>
    <div class="cus-body">
        <el-card style="min-height: 650px;">
            <el-table :data="xcCourseList" style="width: 100%">
                <el-table-column fixed prop="id" label="课程编号" width="150" />
                <el-table-column prop="courseName" label="课程名称" width="120" />
                <el-table-column prop="courseDesc" label="课程描述" width="120" />
                <el-table-column prop="courseTypeStr" label="课程类型" width="120" />
                <el-table-column prop="userId" label="老师编号" width="120" />
                <el-table-column prop="teacherName" label="老师名称" width="120" />
                <el-table-column prop="courseStar" label="课程推荐" width="120" />
                <el-table-column prop="coursePoint" label="课程学分" width="120" />
                <el-table-column prop="courseTimeStr" label="课程时间" width="120" />
                <el-table-column prop="courseSubTotal" label="课程子节" width="120" />
                <el-table-column prop="createTimeStr" label="创建时间" width="120" />
                <el-table-column fixed="right" label="操作" min-width="120">
                    <template #default="{row}">
                        <el-button link type="primary" size="small" @click="editRow(row)">
                            编辑
                        </el-button>
                        <el-button link type="primary" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
  </div>


    <!-- 新增或编辑 -->
    <el-drawer v-model="showDrawer" >
        <template #header>
            <h4>操作</h4>
        </template>
        <template #default>
            <div>
                <el-form
                :model="xcCourseVo" 
                ref="xcCourseVoRef"
                :rules="rules"
                label-width="auto">
                    <el-form-item label="课程名称" :label-position="left" prop="courseName">
                        <el-input v-model="xcCourseVo.courseName" />
                    </el-form-item>
                    <el-form-item label="课程描述" :label-position="left" prop="courseDesc">
                        <el-input v-model="xcCourseVo.courseDesc" />
                    </el-form-item>
                    <el-form-item label="课程类型" :label-position="left" prop="courseType">
                        <el-select v-model="xcCourseVo.courseType" placeholder="请选择">
                        <el-option label="文学" value='1' />
                        <el-option label="科技" value='2' />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="老师编号" :label-position="left" prop="userId">
                        <el-input v-model="xcCourseVo.userId" />
                    </el-form-item>
                    <el-form-item label="老师名称" :label-position="left" prop="teacherName">
                        <el-input v-model="xcCourseVo.teacherName" />
                    </el-form-item>
                    <el-form-item label="课程推荐" :label-position="left" prop="courseStar">
                        <el-input v-model="xcCourseVo.courseStar" />
                    </el-form-item>
                    <el-form-item label="课程学分" :label-position="left" prop="coursePoint">
                        <el-input v-model="xcCourseVo.coursePoint" />
                    </el-form-item>
                    <el-form-item label="课程时间" :label-position="left" prop="courseTime">
                        <el-date-picker
                        v-model="xcCourseVo.courseTime"
                        type="date"
                        placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="生成课节" :label-position="left">
                        <el-switch
                            v-model="subCourse" 
                            :disabled="xcCourseVo.id">
                        </el-switch>
                    </el-form-item>
                    <el-form-item v-if="subCourse" label="课程子节数" :label-position="left" prop="courseSubTotal">
                        <el-input  v-model="xcCourseVo.courseSubTotal" />
                    </el-form-item>
                </el-form>
            </div>
        </template>
        <template #footer>
        <div style="flex: auto">
            <el-button @click="showDrawer = false">取消</el-button>
            <el-button type="primary" @click="confirmClick">提交</el-button>
        </div>
        </template> 
  </el-drawer>
</template>


<style lang="scss" scoped>
    .flex-row{
        display: flex;
        flex-direction: row;
    }
    .cus-header{
        .search{
            display: flex;
            flex-direction: row;
        }
    }
    .cus-body{
        margin-top: 15px;
    }
</style>